<?php

namespace app\api\controller\yexam;

use app\admin\model\yexam\ComboList;
use app\admin\model\yexam\SubjectCombo;
use app\common\controller\Api;

/**
 * @ApiSector (套餐管理)
 */

class Combo extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle (获取科目套餐列表)
     * @ApiParams (name=subject_id,type=integer,required=true,description=科目id)
     * @ApiParams (name=is_module,type=integer,required=true,description=是否开启模块显示)
     * @ApiParams (name=is_index,type=integer,required=true,description=是否开启首页展示)
     * @ApiMethod (GET)
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function comboList()
    {
        //================ 科目id =================
        $subjectId = $this->request->param('subject_id');
        $combo = new SubjectCombo();
        $module = $this->request->has('is_module');//开启模块显示
        $isIndex = $this->request->request('is_index',0);//==== 开启首页展示
        $where['subject_id'] = $subjectId;
        if ($module)
        {
            $where['is_module'] = 1;
        }
        if ($isIndex) //=== 首页显示的套餐
        {
            $where['is_index'] = 1;
        }
        $list = $combo->order('sort','desc')->where($where)->select();
        foreach ($list as $key => $value) {
            $comboInfo = new ComboList();
            $info = $comboInfo->where('id', $value['combo_id'])->find();
            if ($info['combo_on'] == 0)
            {
                unset($list[$key]);
                continue;
            }
            $list[$key]['combo_jieshao'] = $info['combo_jieshao'];
            $list[$key]['tags'] = explode(" ",$info['combo_tags']);
            $list[$key]['combo_yprice'] = $info['combo_yprice'];
            $list[$key]['combo_price'] = $info['combo_price'];
            $content = json_decode($value['combo_relation'],true);
            $list[$key]['type'] = $content['history'] == 1 ? 'history' : 'fee';
            $list[$key]['combo_img'] = $info['combo_img'];
            $list[$key]['is_share'] = $info['is_share'];
            //============= 此处是为了在题库页面显示历年真题
            /*if (empty($content['history']))
            {
                if (empty($content['formal']) && empty($content['fee']))
                    unset($list[$key]);
            }*/
        }
        $this->success('请求成功',$list);
    }

    /**
     * @ApiTitle (套餐信息)
     * @ApiMethod (GET)
     * @ApiParams (name=combo_id,type=integer,required=true,description=套餐id)
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function comboInfo(){
        $comboId  = $this->request->param('combo_id');
        $combo = new ComboList();
        $info = $combo->where('id',$comboId)->find();
        $info['tags'] = explode(" ",$info['combo_tags']);
        $info['combo_yprice'] = bcadd($info['combo_yprice'],0);
        $info['expire_time'] = date("Y-m-d",strtotime("+".$info['combo_time']."year"));
        $info['combo_type'] = $info['combo_type'] == 1 ? $info['combo_time']."年" : $info['combo_time']."考季";
        $this->success('请求成功',$info);
    }
}